package tool;

import java.io.IOException;

import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
import javax.microedition.media.control.MetaDataControl;

import player.MP3Metadata;
import function.Mp3File;

public class MetadataUtils {

	public static MP3Metadata getMetadata(Mp3File mp3) {

		Player player = null;
		try {
			player = Manager.createPlayer(mp3.getFilePath());
			player.prefetch();
		} catch (IOException e) {
			throw new RuntimeException("MetadataUtils.getMetadata()");
		} catch (MediaException e) {
			throw new RuntimeException("MetadataUtils.getMetadata()");
		}

		MetaDataControl metadatas = (MetaDataControl) player
				.getControl("javax.microedition.media.control.MetaDataControl");
		// title, author, album, date, genre
		MP3Metadata metadata = new MP3Metadata(mp3.getTitle(), metadatas
				.getKeyValue("author"), metadatas.getKeyValue("genre"));
		player.close();

		return metadata;
	}

}
